package top.wardyan.wbaseswt.control.wtable;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.jfinal.aop.Aop;
import com.jfinal.plugin.activerecord.IRow;

import top.wardyan.wbaseswt.action.base.AbstractShell;
import top.wardyan.wbaseswt.conf.Const;
import top.wardyan.wbaseswt.conf.model.SysTableCol;
import top.wardyan.wbaseswt.conf.service.ISysTableColService;
//import top.wardyan.action.base.AbstractShell;
//import top.wardyan.base.model.BaseModel;
//import top.wardyan.conf.Const;
//import top.wardyan.conf.config.SQLite;
//import top.wardyan.conf.model.SysTableCol;
//import top.wardyan.conf.model.SysTableColItem;
//import top.wardyan.conf.service.ISysTableColItemService;
//import top.wardyan.conf.service.ISysTableColService;
//import top.wardyan.util.KeyCode;
//import top.wardyan.util.Msg;
//import top.wardyan.util.ShellUtil;
import top.wardyan.wbaseswt.control.WButton;
import top.wardyan.wbaseswt.control.WText;
import top.wardyan.wbaseswt.util.KeyCode;
import top.wardyan.wbaseswt.util.Msg;
import top.wardyan.wbaseswt.util.ShellUtil;

public class WTableItemSettingShell extends AbstractShell<WTableItemSettingShell>{

	protected int mID;	//	, dID;
	protected WTable mainTable;
	protected SysTableCol main;
	
	protected ISysTableColService service = Aop.get(ISysTableColService.class);
	//protected ISysTableColItemService itemService = SQLite.getInstance().tablecolItemService;
	public WTableItemSettingShell() {
		this(0);
	}
	
	public WTableItemSettingShell(int mID) {
		super();
		this.mID = mID;
		//this.dID = dID;
		this.createContent();
	}
	
	protected WText searchText;
	
	private void createContent() {
		this.setSize(1024, 768);
		ShellUtil.center(this);
		this.setLayout(new FormLayout());
		
		
		searchText = new WText(this, SWT.BORDER, "", "搜索关键字");
		searchText.setTextLimit(64);
		FormData fd_searchText = new FormData();
		fd_searchText.bottom = new FormAttachment(this, 30);
		fd_searchText.left = new FormAttachment(this, 0);
		fd_searchText.right = new FormAttachment(100, -400);
		fd_searchText.top = new FormAttachment(this, 2);
		searchText.setLayoutData(fd_searchText);
		searchText.addModifyListener(new ModifyListener() {			
			@Override
			public void modifyText(ModifyEvent e) {
				mainTable.show("mainTable", searchText.getText());
			}
		});
		//  清空 恢复默认 新增 保存
		WButton btnClear = new WButton(this, SWT.PUSH, "清空", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clear();
			}
		});
		FormData fd_btnClear = new FormData();
		fd_btnClear.left = new FormAttachment(searchText, 2);
		fd_btnClear.top = new FormAttachment(this, 2);
		fd_btnClear.right = new FormAttachment(btnClear, 70);
		fd_btnClear.bottom = new FormAttachment(this, 30);
		btnClear.setLayoutData(fd_btnClear);

		WButton btnSetDefault = new WButton(this, SWT.PUSH, "恢复默认", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setDefault();
			}
		});
		FormData fd_btnSetDefault = new FormData();
		fd_btnSetDefault.left = new FormAttachment(btnClear, 2);
		fd_btnSetDefault.top = new FormAttachment(this, 2);
		fd_btnSetDefault.right = new FormAttachment(btnSetDefault, 90);
		fd_btnSetDefault.bottom = new FormAttachment(this, 30);
		btnSetDefault.setLayoutData(fd_btnSetDefault);
		btnSetDefault.setEnabled(false);
		WButton btnAdd = new WButton(this, SWT.PUSH, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				add();
			}
		});
		FormData fd_btnAdd = new FormData();
		fd_btnAdd.left = new FormAttachment(btnSetDefault, 2);
		fd_btnAdd.top = new FormAttachment(this, 2);
		fd_btnAdd.right = new FormAttachment(btnAdd, 70);
		fd_btnAdd.bottom = new FormAttachment(this, 30);
		btnAdd.setLayoutData(fd_btnAdd);
		WButton btnAutoname = new WButton(this, SWT.PUSH, "自动命名", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				autoname();
			}
		});
		btnAutoname.setEnabled(false);
		FormData fd_btnAutoname = new FormData();
		fd_btnAutoname.bottom = new FormAttachment(this, 30);
		fd_btnAutoname.left = new FormAttachment(btnAdd, 2);
		fd_btnAutoname.right = new FormAttachment(btnAutoname, 90);
		fd_btnAutoname.top = new FormAttachment(this, 2);
		btnAutoname.setLayoutData(fd_btnAutoname);
		
		WButton btn = new WButton(this, SWT.PUSH, "保存", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				saving();
			}
		});
		FormData fd_btn = new FormData();
		fd_btn.bottom = new FormAttachment(this, 30);
		fd_btn.left = new FormAttachment(btnAutoname, 2);
		fd_btn.right = new FormAttachment(btn, 70);
		fd_btn.top = new FormAttachment(this, 2);
		btn.setLayoutData(fd_btn);
		
		mainTable = new WTable(this, SWT.NONE, "mainTable");
		FormData fd_mainCom = new FormData();
		fd_mainCom.bottom = new FormAttachment(100, -1);
		fd_mainCom.left = new FormAttachment(this, 0);
		fd_mainCom.right = new FormAttachment(100, -1);
		fd_mainCom.top = new FormAttachment(searchText, 6);
		mainTable.setLayoutData(fd_mainCom);
		//mainCom.setLayout(new FormLayout());
		
//		List<SysTableColItem> cols = itemService.list(mID);
//		//mainTable.setIsAutoPack(true);
//		if(cols != null) {
//			mainTable.datas = cols;
//			mainTable.show("mainTable", "");
//		}
		mainTable.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(mainTable.getSelectionIndex() >= 0 && e.keyCode == KeyCode.KEY_DELETE) {
					//ISysTableColItemService itemService = SQLite.getInstance().tablecolItemService;
					TableColumn[] cols = mainTable.getColumns();
					
					
					WTableItem tbitem = (WTableItem)mainTable.getItem(mainTable.getSelectionIndex());
					//String sid = tbitem.getText(cindex);
					IRow <? extends IRow <?>> m = tbitem.getModel();
					int mid = m.getInt("mID");
					int did = m.getInt("dID");
					//SysTableColItem tc = itemService.findByIDs(mid, did);
					int tableindex = mainTable.getSelectionIndex();
					// 删除键
//					if(tc != null) {
//						boolean doit = true;
//						if((tc.getStr("xField").equals("sID") || tc.getStr("xField").equals("mID") || tc.getStr("xField").equals("dID"))) {
//							if(Msg.confirm(self, "警告", "字段:" + tc.getStr("xField") + " 貌似是系统字段,删掉后可能会导致严重问题,请确认!", SWT.CANCEL | SWT.OK) != SWT.OK) {
//								doit = false;
//							}
//						}
//						if(doit) {
//							itemService.delete(tc);
//							mainTable.show("mainTable", "", itemService.list(mID));
//							mainTable.select(tableindex - 1);
//						}
//					}
					
				}
			}
		});
		mainTable.addMouseListener(new MouseAdapter() {
			int editColumnIndex = -1;
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				final Point point = new Point(e.x, e.y);
				final TableItem tableItem = mainTable.getItem(point);
				if (tableItem == null) {
					return;
				}
				for (int i = 0; i < mainTable.getColumnCount(); i++) {
					final Rectangle r = tableItem.getBounds(i);
					if (r.contains(point)) {
						editColumnIndex = i;
						final TableEditor editor = new TableEditor(mainTable);
						final Control oldEditor = editor.getEditor();
						if (oldEditor != null) {
							oldEditor.dispose();
						}
						final Text text = new Text(mainTable, SWT.NONE);
						text.computeSize(SWT.DEFAULT, mainTable.getItemHeight());
						editor.grabHorizontal = true;
						editor.minimumHeight = text.getSize().y;
						editor.minimumWidth = text.getSize().x;
						editor.setEditor(text, tableItem, editColumnIndex);
						text.setText(tableItem.getText(editColumnIndex));
						text.forceFocus();
						text.addModifyListener(new ModifyListener() {
							@Override
							public void modifyText(final ModifyEvent e) {
								editor.getItem().setText(editColumnIndex, text.getText());
							}
						});
						text.addFocusListener(new FocusListener() {
							@Override
							public void focusGained(final FocusEvent e) {
								//text.selectAll();
							}
						
							@Override
							public void focusLost(final FocusEvent e) {
								text.setForeground(Const.COLOR_WHITE);
								text.setBackground(Const.COLOR_RED);
							}
						});
						
					}
				}
			}
		});
		main = service.findByIDs(mID);
		
		if(main != null) {
			this.setText(this.getText() + " " + main.getXTitle());
		}
	}
	
	protected void saving() {
		TableItem[] items = mainTable.getItems();
		TableColumn[] cols = mainTable.getColumns(); 
		for(TableItem item : items) {
			int i = 0;
			//String sid = item.getText(0);
			//String index = item.getText(1);
			// 没有sID,说明是新增的 // 2020-02-16 22:51:36 不行,不允许新增, 只允许用按钮新增
			// 不行! 如果调整了顺序呢?
			int mid = 0, did = 0;
			i = 0;
			for(TableColumn c : cols) {
				WTableColumn wc = (WTableColumn)c;
				if(wc.getXField().equals("mID")) {
					mid = Integer.valueOf(item.getText(i));
				}else if(wc.getXField().equals("dID")) {
					did = Integer.valueOf(item.getText(i));
				}
				i ++;
			}
			i = 0;
//			SysTableColItem tc = itemService.findByID(mid, did);
//			if(tc != null) {
//				for(TableColumn c : cols) {
//					WTableColumn wc = (WTableColumn)c;
//					tc.set(wc.getXField(), item.getText(i));
//					i ++;
//				}
//			}
//			itemService.update(tc);
			// SysTableCol tc = new SysTableCol();
			
		}
		Msg.show(self, "OK", "更新完成, 请重新打开窗口");
	}
	
	@Override
	protected String getTitle() {
		return "替换值设置";
	}
	
	public static void main(String[] args) {
		new WTableItemSettingShell().go();
	}

	@Override
	protected boolean isMainWindow() {
		return false;
	}
	
	
	private void clear() {
//		List<SysTableColItem> tcs = itemService.list(mID);
//		if(tcs != null) {
//			for(SysTableColItem t : tcs) {
//				itemService.delete(t);
//			}
//		}
//		mainTable.show("mainTable", "", itemService.list(mID));
		Msg.show(self, "警告", "未支持的功能");
	}
	
	private void setDefault() {
	}
	
	private void add() {
//		SysTableColItem item = new SysTableColItem()
//				.setMID(mID).setSIndex(1).set("xField", main != null ? main.getXField() : "").set("xName", "")
//				.set("xValue", itemService.maxValue(main != null ? main.getXClassName() : "", main != null ? main.getXTableName() : "", main != null ? main.getXField() : ""))
//				.set("xNote", "").set("xClassName", main != null ? main.getXClassName() : "").set("xTableName", main != null ? main.getXTableName() : "");
//		
//		itemService.save(item);
////		this.dispose();
////		new WTableItemSettingShell(mID).go();
//		mainTable.show("mainTable", "", itemService.list(mID));.
		Msg.show(self, "警告", "未支持的功能");
	}

	private void autoname() {
	}
}
